package com.formiaomiao.demain.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.activity.AddDemainActivity;
import com.formiaomiao.demain.base.BaseFragment;
import com.formiaomiao.demain.base.HomeFragmentAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class DemainFragment extends BaseFragment {

    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    Unbinder unbinder;
    @BindView(R.id.fabtn_add)
    FloatingActionButton fabtnAdd;

    private String[] mTitles = {"关注", "全部"};

    public static DemainFragment newInstance() {
        Bundle args = new Bundle();
        DemainFragment fragment = new DemainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.demain_layout, container, false);
        unbinder = ButterKnife.bind(this, v);
        DataInit();
        return v;
    }

    @SuppressLint("ResourceAsColor")
    public void DataInit() {
        HomeFragmentAdapter adapter = new HomeFragmentAdapter(getChildFragmentManager());
        //设置tab的文字
        ArrayList<String> titles = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            titles.add(mTitles[i]);
        }
        adapter.setTitles(titles);
        //设置tab对应得fragment

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(DemainFollowFragment.newInstance());
        fragments.add(DemainAllFragment.newInstance());
        adapter.setFragments(fragments);
        viewPager.setAdapter(adapter);
        tab.setupWithViewPager(viewPager);
        //设置默认第一个为选中的tab
        tab.getTabAt(0).isSelected();
        //自定义tab样式
        for (int i = 0; i < mTitles.length; i++) {
            //获取某个tab(这个API对应自定义TabLayout很重要)
            TabLayout.Tab tabAt = tab.getTabAt(i);
            //加载自定义的tab布局
            View inflate = View.inflate(getActivity(), R.layout.tab_demain_title_layout, null);
            TextView mTabCustomText = (TextView) inflate.findViewById(R.id.tab_custom_text);
            mTabCustomText.setText(mTitles[i]);
            //把自定义的tab布局设置给对应的tab
            tabAt.setCustomView(inflate);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.fabtn_add)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), AddDemainActivity.class));
    }
}
